<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath(); %>

<div class="callout callout-left">
  <span class="callout-title text-muted">注册用户统计</span><!-- &nbsp;<span class="glyphicon glyphicon-question-sign"></span> -->
  <a class="btn btn-primary"   href="javascript:;" id="export_excel" style="margin-left: 35px;"><span class="glyphicon glyphicon-export">&nbsp;导出excel</span></a>
</div>

<div class="callout">
  <form class="form-inline"  >
    <div class="form-group">
      <label for="deptName_search">所属部门：</label>
      <select class="form-control" id="deptName_search" name="deptName_search">
        <option value="">--请选择--</option>
      </select>
    </div>&nbsp;&nbsp;&nbsp;
    <button type="button" class="btn btn-primary" id="doSearch">
      <span class="glyphicon glyphicon-search">&nbsp;搜索</span>
    </button>&nbsp;&nbsp;&nbsp;
  </form>
</div>

<div class="callout callout-white">
<div class="table-responsive center-block" style="max-width: 900px;">
  <table class="table table-bordered" id="t_excel">
    <thead>
      <tr>
        <th>数量（人）</th>
        <th>1月</th>
        <th>2月</th>
        <th>3月</th>
        <th>4月</th>
        <th>5月</th>
        <th>6月</th>
        <th>7月</th>
        <th>8月</th>
        <th>9月</th>
        <th>10月</th>
        <th>11月</th>
        <th>12月</th>
      </tr>
    </thead>
    <tbody id="rows_content">
      
    </tbody>
  </table>
  </div>
</div>

<script>
require.config({
	baseUrl: '<%=path %>/js',
	paths: {
		jquery: 'jquery-1.9.1.min',
		commmon: 'common',
		bootstrapswitch: 'bootstrap-switch.min'
	}
});
require(['jquery', 'common', 'bootstrapswitch'], function($, common) {
//console.log("密钥总量模块载入");

var workspace = function() {

	var dataOpt;
	var users;
	var departments;

	var init = function() {
		dataOpt = common.dataOpt;

		departments = common.ajaxLoadData(dataOpt.departmentOpt);

		_initUsersTable();
    _doSearch();
		common.exportExcel("注册用户统计");
	}

	var _initUsersTable = function() {
		
		var rows = {}

		// 日期分类
		var operateData = function() {
			users = common.ajaxLoadData(dataOpt.userOpt);
			dataOpt.userOpt.data = {}
			users.forEach(function(user) {
				var ymd = user["regTime"].split("-");
				var months = {
					"jan": 0,"feb": 0,"mar": 0,"apr": 0,"may": 0,"june": 0,
					"july": 0,"aug": 0,"sep": 0,"oct": 0,"nov": 0,"dec": 0,
				}

				var monEngl = {
				  "01": "jan", "02": "feb", "03": "mar", "04": "apr", "05": "may",
          "06": "june", "07": "july", "08": "aug", "09": "sep", "10": "oct",
          "11": "nov", "12": "dec"
        }



        if($.isEmptyObject(rows[ymd[0]])) {
          rows[ymd[0]] = months;
        }

        rows[ymd[0]][monEngl[ymd[1]]]++;
			});
		}
		
		// 数据统计
		operateData();
		
		var template = "<tr><th>#year#</th>" +
			"<td>#jan#</td><td>#feb#</td><td>#mar#</td>" +
			"<td>#apr#</td><td>#may#</td><td>#june#</td>" +
			"<td>#july#</td><td>#aug#</td><td>#sep#</td>" +
			"<td>#oct#</td><td>#nov#</td><td>#dec#</td></tr>";
		
		var html = "";
		for(var year in rows) {
			var thisT = template;
			thisT = thisT.replace(/#year#/g, year).replace(/#jan#/g, rows[year]["jan"]).replace(/#feb#/g, rows[year]["feb"]).replace(/#mar#/g, rows[year]["mar"])
					.replace(/#apr#/g, rows[year]["apr"]).replace(/#may#/g, rows[year]["may"]).replace(/#june#/g, rows[year]["june"]).
					replace(/#july#/g, rows[year]["july"]).replace(/#sep#/g, rows[year]["sep"]).replace(/#oct#/g, rows[year]["oct"]).
					replace(/#nov#/g, rows[year]["nov"]).replace(/#dec#/g, rows[year]["dec"]).replace(/#aug#/g, rows[year]["aug"]);
			html += thisT;
		}
		$("#rows_content").html(html);
		
	}

	var _doSearch = function() {
    // 部门
    departments.forEach(function(dept) {
        var opt = document.createElement("option");
        $(opt).val(dept["deptName"]);
        $(opt).text(dept["deptName"]);
        $("#deptName_search").append(opt);
    });

    $("#doSearch").click(function() {
      dataOpt.userOpt.data = {
          "deptName": encodeURI(encodeURI($("#deptName_search").val()))
      };
      _initUsersTable();
    });
  }

	return {
		init: init
	}
}();
	
$(function() { 	workspace.init(); });
});
requirejs.onError = function (err) {  
  console.error(err);
  if (err.requireType === 'timeout') {
      console.error('modules: ' + err.requireModules);
  }
  throw err;
};
</script>